from sqlalchemy.orm import sessionmaker
from api.models.api_models import FacePicture


class FacePictureDAO:
    def __init__(self, engine):
        self.engine = engine
        self.Session = sessionmaker(bind=self.engine, expire_on_commit=False)

    def create(self, face_picture_id, user_id, face_picture_path, upload_time=None):
        session = self.Session()
        face_picture = FacePicture(face_picture_id=face_picture_id, user_id=user_id, face_picture_path=face_picture_path, upload_time=upload_time)
        session.add(face_picture)
        session.commit()
        session.close()
        return face_picture

    def read(self, face_picture_id):
        session = self.Session()
        face_picture = session.query(FacePicture).filter_by(face_picture_id=face_picture_id).first()
        session.close()
        return face_picture

    def update(self, face_picture_id, user_id=None, face_picture_path=None, upload_time=None):
        session = self.Session()
        face_picture = session.query(FacePicture).filter_by(face_picture_id=face_picture_id).first()
        if user_id is not None:
            face_picture.user_id = user_id
        if face_picture_path is not None:
            face_picture.face_picture_path = face_picture_path
        if upload_time is not None:
            face_picture.upload_time = upload_time
        session.commit()
        session.close()

    def delete(self, face_picture_id):
        session = self.Session()
        face_picture = session.query(FacePicture).filter_by(face_picture_id=face_picture_id).first()
        session.delete(face_picture)
        session.commit()
        session.close()